package com.shop.shop.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.shop.shop.entity.User;
import com.shop.shop.service.UserService;
import com.shop.shop.utile.JWTUtils;
import com.shop.shop.utile.Q;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@Slf4j
@RestController
@RequestMapping("/shop/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Q login(@RequestBody(required = false) User user) {
        User user1 = userService.login(user);
        //生成token
        if (user1 != null) {
            HashMap<String, String> pload = new HashMap<>();
            pload.put("name", user1.getUrName());
            pload.put("phone", user1.getUrPhone().toString());
            pload.put("image", user1.getUrImage());
            pload.put("sex", user1.getUrSex().toString());
            pload.put("id", user1.getUrId().toString());
            String token = JWTUtils.getToken(pload);
            return Q.ok().data("user", user1).data("token", token);
        }
        return Q.error();
    }

    /**
     * 根据token获取用户的信息
     *
     * @param token
     * @return
     */
    @PostMapping("/token")
    public Q test(@RequestBody String token) {
        //log.info("当前的token :"+token);
        Map<String, Object> map1 = JWTUtils.verify(token);

        return Q.ok().data(map1);
    }

    /**
     * 用户修改信息
     */

    @PostMapping("updateUser")
    public Q updateUser(@RequestBody User user) {
        boolean b = userService.updateById(user);
        return b ? Q.ok() : Q.error();
    }


    /**
     * 用户密码修改
     */
    @GetMapping("updatePassword")
    public Q updatePassword(  Integer id, String oldPassword,  String urPassword) {
         // Integer id1=Integer.parseInt(id);
        int i = userService.updatePaaword(oldPassword, urPassword, id);
        return i > 0 ? Q.ok() : Q.error();
    }


    /*
    *
    *
    *
    *
    *   @GetMapping("/token/s")
    public  HashMap<String, Object>  test(String token){
        log.info("当前的token :"+token);
        HashMap<String, Object> map = new HashMap<>();
           DecodedJWT verify = JWTUtils.verify(token);
            map.put("status","成功");
       return map;
    }
    @GetMapping("/token/ss")
    public  HashMap<String, Object>  test1(String token){
        HashMap<String, Object> map = new HashMap<>();
        log.info("当前的token :"+token);
        map.put("status","成功");
        return map;
    }
    *
    *
    *
    *
    *
    * */
}

